package org.steve.betterattack;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import org.steve.betterattack.Listener.WeaponListener;
import org.steve.betterattack.Listener.PlayerEventsListener;
import org.steve.betterattack.PluginCommandExecutor.AdminCommandExecutor;
import org.steve.betterattack.PluginCommandExecutor.BACommandExecutor;

import java.util.*;

public class BetterAttack extends JavaPlugin {
    public static String pluginInfo=
            "BetterAttack - Use more diversified attack methods on Minecraft Servers\n" +
            "By Steve(Liyan Zhao)\n" +
            "Author's gitee: https://gitee.com/zhaoliyan\n" +
            "Author's github: https://github.com/zly2006\n" +
            "This plugin is open-sourced on gitee(the repository on github is unavailable now)\n" +
            "";
    public Random random=new Random(new Random().nextInt());
    public boolean debug=false;
    FileConfiguration config;
    public Map<String,Weapons>RegisteredWeapons;
    public Map<String,Items>RegisteredItems;
    public Map<String,CraftingRecipe>RegisteredCraftingRecipe;

    @Override
    public void onEnable(){
        if (!getServer().getPluginManager().isPluginEnabled("Essential")){

        }

        GUI.init(this);
        this.getCommand("betterattackadmin").setExecutor(new AdminCommandExecutor(this));
        this.getCommand("betterattack").setExecutor(new BACommandExecutor(this));
        this.getServer().getPluginManager().registerEvents(new WeaponListener(this),this);
        this.getServer().getPluginManager().registerEvents(new GUIListener(),this);
        this.getServer().getPluginManager().registerEvents(new PlayerEventsListener(),this);

        config = this.getConfig();
        this.saveDefaultConfig();

        RegisteredCraftingRecipe=new HashMap<>();
        RegisteredItems=new HashMap<>();
        RegisteredWeapons=new HashMap<>();
        if(config.contains("debug"))debug=config.getBoolean("debug");
        if(config.contains("weapon")) {
            getLogger().info("Registering weapons...");
            Set<String> weapons = config.getConfigurationSection("weapon").getKeys(false);
            for (String path : weapons) {
                try {
                    getLogger().info("Registering weapon:" + path);
                    RegisteredWeapons.put(path, new Weapons(this, config.getConfigurationSection("weapon").getConfigurationSection(path)));
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        }
        if(config.contains("item")) {
            getLogger().info("Registering items...");
            Set<String> items = config.getConfigurationSection("item").getKeys(false);
            for (String path : items) {
                try {
                    getLogger().info("Registering item:" + path);
                    RegisteredItems.put(path, new Items(this, config.getConfigurationSection("item").getConfigurationSection(path)));
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        }
        if(config.contains("recipe")) {
            getLogger().info("Registering recipes...");
            Set<String> recipes = config.getConfigurationSection("recipe").getKeys(false);
            for (String path : recipes) {
                try {
                    getLogger().info("Registering recipe:" + path);
                    RegisteredCraftingRecipe.put(path, new CraftingRecipe(this, config.getConfigurationSection("recipe").getConfigurationSection(path)));
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        }
        getLogger().info("Geyser玩家可能出现严重问题，请谨慎使用！");
    }

    @Override
    public void onDisable(){
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        return BACommandExecutor.onTabComplete(sender,command,alias,args);
    }
}